package com.lv.phuotbacnam.ui.adapter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.queries.QueriesConstant;
import com.lv.phuotbacnam.ui.fragment.MyItem_Baidang;
import com.lv.phuotbacnam.ui.fragment.MyItem_Diadiem;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.parse.ParseObject;

public class TListViewApdapterDiadiem extends BaseAdapter
{
	private Activity activity;
	private ArrayList<ParseObject> arrItem;
	private static LayoutInflater inflater=null;
	
	
	public TListViewApdapterDiadiem(ArrayList<ParseObject> arrDiadiem, Activity activity) {
		super();
		this.arrItem = arrDiadiem;
		this.activity = activity;
		inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.arrItem.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		Log.e("position",position +"");
		Log.e("size of arraylist", arrItem.size()+"");
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_listview_diadiem, null);
		}
		
		
		ImageView imgitem=(ImageView) convertView.findViewById(R.id.image_diadiem); 		//image
		TextView tv_tendiadiem = (TextView)convertView.findViewById(R.id.tv_tendiadiem);	//ten bai dang
		TextView tv_mota = (TextView)convertView.findViewById(R.id.tv_mota);				//mo ta
		
		ParseObject diadiem = arrItem.get(position);
		String image = "";
		JSONArray list_photo = diadiem.getJSONArray(QueriesConstant.TableDiaDiem.COLUMN_list_photo);
		try {
			JSONObject first_photo = list_photo.getJSONObject(0);
			tv_tendiadiem.setText(arrItem.get(position).getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem));
			tv_mota.setText(arrItem.get(position).getString(QueriesConstant.TableDiaDiem.COLUMN_mo_ta));
			image = first_photo.getString("url");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		
		ImageLoader imageLoader;
		imageLoader = ImageLoader.getInstance();
		
		//cache image
		DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
						.cacheOnDisc(true).resetViewBeforeLoading(true).build();
		
        //Initialize ImageLoader with configuration. Do it once.
        imageLoader.init(ImageLoaderConfiguration.createDefault(parent.getContext()));
		imageLoader.displayImage(image,imgitem,options);
        //imageLoader.displayImage(image,imgitem);
		return convertView;
	}

}
